package co.fitstart.entity.user;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.user.enums.StudentTargetType;

/**
 *
 * @author Ark
 *
 */
@Entity
@Table(name = "student_target_data")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class StudentTargetData {

    @Id
    @GeneratedValue
    private Long id;
    
    /** 对应学员 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Student student;
    
    /** 所属课次 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private CourseRound courseRound;
    
    /** 阶段目标 */
    private String shortTarget;
    
    /** 长期目标 */
    private String longTarget;
    
    /** 创建时间 */
    private Date createTime;

    @Column(name = "is_disabled")
    private boolean disabled;
    
    /** 操作人id */
    private Long operator;
    
    /** 操作时间 */
    private Date operateTime;
    
    /** 目标类型 */
    @Enumerated(EnumType.ORDINAL)
    private StudentTargetType targetType;
    
    /** 具体数值 */
    private String numericalValue;
    
    /** 体重变化 */
    private Integer weightChange;
    
    /** 体脂变化 */
    private Integer bfrChange;
    
    /** 具体目标 */
    private String description;
    
    /**
     * The default constructor
     */
    public StudentTargetData() {
        
    }
    
    public StudentTargetData(CourseRound courseRound) {
        this.student = courseRound.getStudent();
        this.courseRound = courseRound;
        this.createTime = new Date();
        this.disabled = false;
    }
    
    /**
     * @param student
     * @param courseRound
     * @return
     */
    public StudentTargetData initialize(Student student, CourseRound courseRound) {
        this.student = student;
        this.courseRound = courseRound;
        this.createTime = new Date();
        this.disabled = false;
        
        if(this.targetType != null) {
            if(this.targetType.isDigital()) {
                this.description = null;
                if(this.targetType.isWeightLosing()) {
                    this.bfrChange = null;
                } else if(this.targetType.isBodyFatRateReducing() && (this.weightChange == null || this.weightChange == 0)) {
                    this.weightChange = null;
                }
            } else {
                this.bfrChange = null;
                this.weightChange = null;
            }
        }
        
        return this;
    }
    
    /**
     * edit
     * @param shortTarget
     * @param longTarget
     * @param operator
     * @return
     */
    public StudentTargetData edit(String shortTarget, String longTarget, Long operator) {
        this.shortTarget = shortTarget;
        this.longTarget = longTarget;
        this.operator = operator;
        this.operateTime = new Date();
        return this;
    }

    /**
     * 
     * @param targetType
     * @param weightChange
     * @param bfrChange
     * @param description
     * @return
     */
    public StudentTargetData edit(StudentTargetType targetType, Integer weightChange, Integer bfrChange, String description) {
        this.targetType = targetType;
        if(targetType != null) {
            if(targetType.isDigital()) {
                this.description = null;
                
                if(targetType.isBodyFatRateReducing() && (weightChange == null || weightChange == 0)) {
                    this.weightChange = null;
                } else {
                    this.weightChange = weightChange;
                }
                
                if(targetType.isWeightLosing()) {
                    this.bfrChange = null;
                } else {
                    this.bfrChange = bfrChange;
                }
            } else {
                this.bfrChange = null;
                this.weightChange = null;
                this.description = description;
            }
        }
        
        this.operateTime = new Date();
        return this;
    }

    /**
     * 
     * @return
     */
    public String getTarget() {
        if(targetType != null) {
            if(targetType.isBodyFatRateReducing()) {
                if(this.weightChange == null || this.weightChange == 0) {
                    return "体脂减少" + bfrChange + "%";
                } else {
                    return "体脂减少" + bfrChange + "%，体重减少" + weightChange + "KG";
                }
            } else if(targetType.isWeightLosing()) {
                return "体重减少" + weightChange + "kg";
            } else if(targetType.isMuscleBuilding()) {
                return "体脂减少" + bfrChange + "%，体重增加" + weightChange + "KG";
            } else {
                return targetType.getDescription();
            }
        } else {
            return null;
        }
    }

    public boolean isDigital() {
        return targetType == null ? false : targetType.isDigital();
    }

    public String getTargetTypeDesc() {
        return targetType == null ? null : targetType.getDescription();
    }
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the student
     */
    public Student getStudent() {
        return student;
    }

    /**
     * @param student the student to set
     */
    public void setStudent(Student student) {
        this.student = student;
    }

    /**
     * @return the courseRound
     */
    public CourseRound getCourseRound() {
        return courseRound;
    }

    /**
     * @param courseRound the courseRound to set
     */
    public void setCourseRound(CourseRound courseRound) {
        this.courseRound = courseRound;
    }

    /**
     * @return the shortTarget
     */
    public String getShortTarget() {
        return shortTarget;
    }

    /**
     * @param shortTarget the shortTarget to set
     */
    public void setShortTarget(String shortTarget) {
        this.shortTarget = shortTarget;
    }

    /**
     * @return the longTarget
     */
    public String getLongTarget() {
        return longTarget;
    }

    /**
     * @param longTarget the longTarget to set
     */
    public void setLongTarget(String longTarget) {
        this.longTarget = longTarget;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    /**
     * @return the operator
     */
    public Long getOperator() {
        return operator;
    }

    /**
     * @param operator the operator to set
     */
    public void setOperator(Long operator) {
        this.operator = operator;
    }

    /**
     * @return the operateTime
     */
    public Date getOperateTime() {
        return operateTime;
    }

    /**
     * @param operateTime the operateTime to set
     */
    public void setOperateTime(Date operateTime) {
        this.operateTime = operateTime;
    }

    /**
     * @return the targetType
     */
    public StudentTargetType getTargetType() {
        return targetType;
    }

    /**
     * @param targetType the targetType to set
     */
    public void setTargetType(StudentTargetType targetType) {
        this.targetType = targetType;
    }

    /**
     * @return the numericalValue
     */
    public String getNumericalValue() {
        return numericalValue;
    }

    /**
     * @param numericalValue the numericalValue to set
     */
    public void setNumericalValue(String numericalValue) {
        this.numericalValue = numericalValue;
    }

    /**
     * @return the weightChange
     */
    public Integer getWeightChange() {
        return weightChange;
    }

    /**
     * @param weightChange the weightChange to set
     */
    public void setWeightChange(Integer weightChange) {
        this.weightChange = weightChange;
    }

    /**
     * @return the bfrChange
     */
    public Integer getBfrChange() {
        return bfrChange;
    }

    /**
     * @param bfrChange the bfrChange to set
     */
    public void setBfrChange(Integer bfrChange) {
        this.bfrChange = bfrChange;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }
}
